﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.Collections.Generic;

public partial class TotalPlan_QuarterPlanDetail : System.Web.UI.Page
{

    Authentication dllAuthentication = new Authentication();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    QuarterPlanDetailManager dllQuarterPlanDetailManager = new QuarterPlanDetailManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        Model.UserInfo objUserInfo = new Model.UserInfo();
        if (objUserInfo.UserName != "admin")
        {
            this.btnDel.Enabled = false;
        }
        if (!IsPostBack)
        {
            bindQuarterPlanGridView();
        }
    }

    /// <summary>
    /// 绑定年计划GridView
    /// </summary>
    /// <author>Grokyao</author>
    /// <date>2009-2-6</date>
    void bindQuarterPlanGridView()
    {
        object[] _value = new object[1];
        _value[0] = Utility.HttpHelp.getParamsInt("QuarterPlanId");
        this.gvlist.DataSource = dllQuarterPlanofManager.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
        this.gvlist.DataBind();
        #region    截取字符串处理，是否显示查看年分解按钮
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            TextBox txtCompleteTime = (TextBox)this.gvlist.Rows[i].Cells[2].FindControl("txtCompleteTime");
            TextBox txtWorkItem = (TextBox)this.gvlist.Rows[i].Cells[3].FindControl("txtWorkItem");
            txtCompleteTime.Text = Convert.ToDateTime(this.gvlist.DataKeys[i]["CompleteTime"]).ToShortDateString();
            txtWorkItem.Text = this.gvlist.DataKeys[i]["WorkItem"].ToString();
            object[] _valueQuarterPlanResolve = new object[1];
            _valueQuarterPlanResolve[0] = this.gvlist.DataKeys[i]["QuarterPlanId"].ToString();
            DataTable _quarterResolve = dllQuarterPlanDetailManager.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _valueQuarterPlanResolve);
            if (_quarterResolve.Rows.Count > 0)
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = true;
                lbl.Visible = false;
            }
            else
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = false;
                lbl.Visible = true;
            }

        }
        #endregion
    }


    /// <summary>
    /// 绑定年分解GridView
    /// </summary>
    /// <author>Grokyao</author>
    /// <date>2009-2-6</date>
    void bindQuarterResolveGridView(string _quarterPlanId)
    {
        object[] _value = new object[1];
        _value[0] = _quarterPlanId;
        DataTable _quarterResolve = dllQuarterPlanDetailManager.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _value);
        this.gdvQuarterResolve.DataSource = _quarterResolve;
        this.gdvQuarterResolve.DataBind();
        for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        {
            string _Lengthstart = this.gdvQuarterResolve.Rows[i].Cells[3].Text;
            string _Lengthend = this.gdvQuarterResolve.Rows[i].Cells[4].Text;
            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.gdvQuarterResolve.Rows[i].Cells[3].Text = _Lengthstart;
            }
            if (_Lengthend.Length > 9)
            {
                _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.gdvQuarterResolve.Rows[i].Cells[4].Text = _Lengthend;
            }
        }
    }



    /// <summary>
    /// 查看年分解按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuarterPlanResolve_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        string _quarterPlanId = this.gvlist.DataKeys[i]["QuarterPlanId"].ToString();
        string _quarterSortId = this.gvlist.Rows[i].Cells[0].Text;

        string _url = "QuarterResolveForMonth.aspx?_quarterPlanId=" + _quarterPlanId + "&_quarterPlanSortId=" + _quarterSortId;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=200,width=900,top=200,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");


    }


    /// <summary>
    /// 修改事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引       
        TextBox txtCompleteTime = (TextBox)this.gvlist.Rows[_rowIndex].Cells[2].FindControl("txtCompleteTime");
        TextBox txtWorkItem = (TextBox)this.gvlist.Rows[_rowIndex].Cells[3].FindControl("txtWorkItem");
        object[] _value = new object[3];
        //时间
        _value[0] =Convert.ToInt32(this.gvlist.DataKeys[_rowIndex]["QuarterPlanId"]);
        _value[1] = txtWorkItem.Text.Trim();
        _value[2] = Convert.ToDateTime(txtCompleteTime.Text.Trim());
        int result = dllQuarterPlanofManager.editQuarterPlanWithTotalPlanByQuarterPlanId(_value);
        if (result == 0)
        {
            Utility.HttpHelp.alert("修改年计划未成功，请重试！", Page);
        }
        bindQuarterPlanGridView();
        string url = Utility.HttpHelp.getParamsString("Url");
        string year = Utility.HttpHelp.getParamsString("TotalYear");
        Utility.HttpHelp.closeWindow(url + "?TotalYear=" + year, Page);
    }

    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {        
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {               
                int QuarterPlanId =Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"]);
                object[] _value = new object[1];
                _value[0] = QuarterPlanId;
                _valueList.Add(_value);
            }
        }
        if (_valueList.Count > 0)
        {
            int result = dllQuarterPlanofManager.delQuarterPlanWithTotalPlanByQuarterPlanId(_valueList);
            if (result == 0)
            {
                Utility.HttpHelp.alert("删除年计划未成功，请重试！", Page);
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择要删除的年计划！", Page);
        }
        bindQuarterPlanGridView();
        if (this.gvlist.Rows.Count == 0)
        {
            string url = Utility.HttpHelp.getParamsString("Url");
            string year = Utility.HttpHelp.getParamsString("TotalYear");
            Utility.HttpHelp.closeWindow(url + "?TotalYear=" + year, Page);
        }
    }
}
